﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;
using System.Diagnostics;
using DotNetAgent.Core;

namespace DotNetAgent
{
    [RunInstaller(true)]
    public partial class DotNetAgentServiceInstaller : Installer
    {
        protected System.ServiceProcess.ServiceProcessInstaller AgentService_ServiceProcessInstaller;
        protected System.ServiceProcess.ServiceInstaller AgentService_ServiceInstaller;
        protected FileLogConnector InstallerLog;

		public DotNetAgentServiceInstaller()
        {
            //Initialise("AgentContainerService", "Agent Container Service", "Container that hosts Compatible Agents", System.ServiceProcess.ServiceAccount.User, "IT\\DotNetAgent", "123456");
			Initialise("DotNetAgentService", "DotNetAgent Service", "Hosts DotNetAgent", System.ServiceProcess.ServiceAccount.User, ".\\dotnetagent", "d0#n3#@g3n#");
            //Initialise("DotNetAgentsService", "DotNetAgents Service", "Hosts DotNet Agents", System.ServiceProcess.ServiceAccount.User, "IT\\DotNetAgent", "123456");
        }

		protected int Initialise(String ServiceName, String DisplayName, String Description)
		{
			return Initialise(ServiceName, DisplayName, Description, ServiceAccount.NetworkService, null, null);
		}

		protected int Initialise(String ServiceName, String DisplayName, String Description, ServiceAccount Account, String Username, String Password)
		{
			String SubOperation = "Start";
			try
			{
				SubOperation = "Event Log Creation";
				InstallerLog = new FileLogConnector(null, new Dictionary<String, Object>()
						{
							{ "Name", "AgentInstaller" }, 
							{ "Filename", "../log/" + ServiceName + "Installer.log" } 
						});
				InstallerLog.LogInfo("AgentInstaller","Initialise", "------------------------------------------------------------------------------------------------------------");

				SubOperation = "Component Initialisation";
				InitializeComponent();

				SubOperation = "Installer Instantiation";
				AgentService_ServiceProcessInstaller = new System.ServiceProcess.ServiceProcessInstaller();
				AgentService_ServiceInstaller = new System.ServiceProcess.ServiceInstaller();

				// 
				// AgentProcessInstaller
				// 
				SubOperation = "AgentProcessInstaller Configuration";
				AgentService_ServiceProcessInstaller.Account = Account;
				AgentService_ServiceProcessInstaller.Password = Password;
				AgentService_ServiceProcessInstaller.Username = Username;
				
				// 
				// AgentServiceInstaller
				// 
				SubOperation = "AgentServiceInstaller Configuration";
				AgentService_ServiceInstaller.Description = Description;
				AgentService_ServiceInstaller.DisplayName = DisplayName;
				AgentService_ServiceInstaller.ServiceName = ServiceName;
				AgentService_ServiceInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
				
				// 
				// ProjectInstaller
				// 
				SubOperation = "ProjectInstaller Configuration";
				Installers.AddRange(new System.Configuration.Install.Installer[] {
                AgentService_ServiceInstaller,
                AgentService_ServiceProcessInstaller});

				return 1;
			}
			catch (Exception ex)
			{
				InstallerLog.LogError("AgentInstaller","Initialise", "Error During " + SubOperation, ex);
				return -1;
			}
		}
	}
}